package com.grom.constant;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.eclipse.core.internal.resources.File;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.Workbench;

public abstract class FileUtil {
	public static InputStream getSelectedStream() {
		IStructuredSelection structured = (IStructuredSelection) PlatformUI.getWorkbench().getActiveWorkbenchWindow()
				.getSelectionService().getSelection("org.eclipse.jdt.ui.PackageExplorer");
		File selected = (File) structured.getFirstElement();
		try {
			return new FileInputStream(selected.getLocation().toFile());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static java.io.File getWorkSpaceLocation() {
		IWorkspace workspace = ResourcesPlugin.getWorkspace();
		IWorkspaceRoot root = (IWorkspaceRoot) workspace.getRoot();
		return root.getLocation().toFile();
	}

	public static IProject getCurrentProject() {
		ISelectionService selectionService = Workbench.getInstance().getActiveWorkbenchWindow().getSelectionService();
		ISelection selection = selectionService.getSelection();
		IProject project = null;
		if (selection instanceof IStructuredSelection) {
			Object element = ((IStructuredSelection) selection).getFirstElement();

			if (element instanceof IResource) {
				project = ((IResource) element).getProject();
			}
		}
		return project;
	}

	public static String getCurrentProjectAbsolutePath() {
		IProject project = getCurrentProject();
		return project.getLocation().toFile().getAbsolutePath();
	}

}
